@SETLOCAL
@ECHO OFF
REM -- Usage: Make <target>...
REM -- E.g.:  Make deb
REM --
REM -- Each <target> may be:
REM --   all		Build everything (default)
REM --   deb		Build debug version
REM --   rel		Build release version
REM --   doc		Generates documentation
REM --   getdsf		Copies documentation support files to DocFiles to simplify static file editing
REM --   drop		Creates a drop directory
REM --   clean		Delete built files
REM --   cleanall	Delete built files and IDE temporary files
REM --

REM -- location of the CodeDoc tool and its source code
SET CODEDOCSRC=..\CodeDoc
SET CODEDOCEXE=%CODEDOCSRC%\bin\Release\CodeDoc.exe

REM -- projects to build
SET Projects=
SET Projects=%Projects% ClientLibrary
SET Projects=%Projects% Client
SET Projects=%Projects% ServerLibrary
SET Projects=%Projects% Server
SET Projects=%Projects% Drivers\Aggregator
SET Projects=%Projects% Drivers\Aquos
SET Projects=%Projects% Drivers\Asp
SET Projects=%Projects% Drivers\Jds
SET Projects=%Projects% Drivers\Lutron
SET Projects=%Projects% Drivers\MusicPlayer
SET Projects=%Projects% Drivers\Panasonic
SET Projects=%Projects% Drivers\Sunfire
SET Projects=%Projects% Simulators\Aquos
SET Projects=%Projects% Simulators\Asp
SET Projects=%Projects% Simulators\Jds
SET Projects=%Projects% Simulators\Lutron
SET Projects=%Projects% Simulators\MusicServer
SET Projects=%Projects% Simulators\Panasonic
SET Projects=%Projects% Simulators\Statnet
SET Projects=%Projects% Simulators\Sunfire
SET Projects=%Projects% Simulators\Unresponsive
SET Projects=%Projects% SimulationOnClient
SET Projects=%Projects% TouchScreen
SET Projects=%Projects% VSerial
SET Projects=%Projects% MusicClientLibrary
REM -- MusicServer won't build on Win2k3 due to Media Player 11 dependency, so build it last:
SET Projects=%Projects% MusicServer

REM -- built .exe/.dll files to include in each drop
SET Parts=
SET Parts=%Parts% Server\bin\DEBREL\HomeUX.exe

REM -- if no targets specified, do "make all"
IF "%1"=="" %~nx0 all

:loop
IF "%1"=="" EXIT /B 0
ECHO [%1]
GOTO %1

:all
CALL %~nx0 deb rel drop
GOTO next

:deb
CALL :MakeEachProject deb
GOTO next

:rel
CALL :MakeEachProject rel
GOTO next

:doc
PUSHD
CALL :MakeEachProject doc
POPD
ECHO -- General --
CALL %~nx0 getdsf
IF ERRORLEVEL 1 EXIT /B %ERRORLEVEL%
xcopy DocFiles Drop /i /s /y
IF ERRORLEVEL 1 EXIT /B %ERRORLEVEL%
GOTO next

:getdsf
copy /y %CODEDOCSRC%\SupportFiles\*.css DocFiles
GOTO next

:drop
rmdir /s /q Drop 2> nul
CALL %~nx0 deb
IF ERRORLEVEL 1 EXIT /B %ERRORLEVEL%
CALL %~nx0 doc
IF ERRORLEVEL 1 EXIT /B %ERRORLEVEL%
:drop2
mkdir Drop\Demo\bin\Debug
copy /y Client\bin\Debug\HomeUX.Client.xap Drop\Demo\bin\Debug
copy /y Client\Simulation.htm Drop\Demo\Default.htm
GOTO next

REM -- drop documentation --
CALL %~nx0 doc
IF ERRORLEVEL 1 EXIT /B %ERRORLEVEL%

REM -- drop demo --
REM TODO...

REM == REM -- drop debug executables --
REM == mkdir Drop\Debug
REM == copy /y ServerLibrary\bin\Debug\HomeUX.dll Drop\Debug
REM == SET DebugParts=%Parts:DEBREL=Debug%
REM == FOR %%F IN (%DebugParts%) DO copy /y %%F Drop\Debug
REM == SET DebugPdb=%DebugParts:dll=pdb%
REM == SET DebugPdb=%DebugPdb:exe=pdb%
REM == FOR %%F IN (%DebugPdb:dll=pdb%) DO copy /y %%F Drop\Debug

REM == REM -- drop release executables --
REM == mkdir Drop\Release
REM == copy /y ServerLibrary\bin\Release\HomeUX.dll Drop\Release
REM == SET ReleaseParts=%Parts:DEBREL=Release%
REM == FOR %%F IN (%ReleaseParts%) DO copy /y %%F Drop\Release
REM == SET ReleasePdb=%ReleaseParts:dll=pdb%
REM == SET ReleasePdb=%ReleasePdb:exe=pdb%
REM == FOR %%F IN (%ReleasePdb:dll=pdb%) DO copy /y %%F Drop\Release

REM == REM -- drop source code
REM == mkdir Drop\SourceCode
REM == copy /y makefile Drop\SourceCode
REM == xcopy Devices Drop\SourceCode\Devices /i /s /exclude:XcopyExclude.txt
REM == xcopy Simulators Drop\SourceCode\Simulators /i /exclude:XcopyExclude.txt
REM == xcopy Simulators\Asp Drop\SourceCode\Simulators\Asp /i /s /exclude:XcopyExclude.txt
REM == xcopy Simulators\Lutron Drop\SourceCode\Simulators\Lutron /i /exclude:XcopyExclude.txt
REM == xcopy Simulators\Lutron\Properties Drop\SourceCode\Simulators\Lutron\Properties /i /exclude:XcopyExclude.txt
REM == mkdir Drop\SourceCode\Simulators\Lutron\LutronExport
REM == copy Simulators\Lutron\LutronExport\LutronData.cs Drop\SourceCode\Simulators\Lutron\LutronExport
REM == xcopy Simulators\Unresponsive Drop\SourceCode\Simulators\Unresponsive /i /s /exclude:XcopyExclude.txt
REM == xcopy VSerial Drop\SourceCode\VSerial /i /s /exclude:XcopyExclude.txt

GOTO next

:clean
CALL :MakeEachProject clean
GOTO next

:cleanall
CALL :MakeEachProject cleanall
ECHO -- General --
rmdir /s /q Drop 2> nul
FOR %%f IN (%CODEDOCSRC%\SupportFiles\*.css) DO del DocFiles\%%~nxf
del Drop.zip 2> nul
del DocFiles\_CodeDoc.css 2> nul
del License.txt 2> nul
attrib -h -r Doc.suo > nul
del Doc.suo 2> nul
rmdir /s /q bin 2> nul
rmdir /s /q obj 2> nul
GOTO next

EXIT /B 0

:next
IF ERRORLEVEL 1 EXIT /B %ERRORLEVEL%
SHIFT
GOTO loop

:MakeEachProject
@ECHO OFF
FOR %%P IN (%Projects%) DO (
	@ECHO OFF
	ECHO -- %%P --
	PUSHD %%P
	CALL %~nx0 %1
	IF ERRORLEVEL 1 EXIT /B %ERRORLEVEL%
	POPD
)
EXIT /B 0

